﻿/**
 * InfoBuilder.cs
 *
 * Copyright (C) 2008,  iron9light
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Collections;
using System.Reflection;

namespace NUnitExtension.DataDriven.AddIn
{
    public class InfoBuilder
    {
        public static TestCaseInfo CreateTestCaseInfo(object testCaseItem)
        {
            if (testCaseItem == null)
                throw new ArgumentNullException("testCaseItem");

            object[] arguments = DataDrivenFramework.GetPropertyValue(testCaseItem, "Arguments") as object[];
            IList constraintList = DataDrivenFramework.GetPropertyValue(testCaseItem, "Constraints") as IList;
            string testName = DataDrivenFramework.GetPropertyValue(testCaseItem, "TestName") as string;
            string description = DataDrivenFramework.GetPropertyValue(testCaseItem, "Description") as string;

            object[] constraints = null;
            if (constraintList != null && constraintList.Count > 0)
            {
                constraints = new object[constraintList.Count];
                int index = 0;
                foreach (object constraint in constraintList)
                    constraints[index++] = constraint;
            }

            return new TestCaseInfo(arguments, constraints, testName, description);
        }

        public static FactoryInfo CreateFactoryInfo(MethodInfo method, Attribute attribute)
        {
            if (method == null)
                throw new ArgumentNullException("method");

            if (attribute == null)
                throw new ArgumentNullException("attribute");

            Type factoryType = DataDrivenFramework.GetPropertyValue(attribute, "FactoryType") as Type;
            object[] args = DataDrivenFramework.GetPropertyValue(attribute, "Arguments") as object[];

            IEnumerable dataSource = DataDrivenFramework.GetDataSource(factoryType, args);

            TestCaseInfo[] testCaseInfos = null;
            if(dataSource != null)
            {
                ArrayList list = new ArrayList();
                foreach (object testCaseItem in dataSource)
                {
                    TestCaseInfo testCaseInfo = InfoBuilder.CreateTestCaseInfo(testCaseItem);
                    list.Add(testCaseInfo);
                }
                testCaseInfos = list.ToArray(typeof (TestCaseInfo)) as TestCaseInfo[];
            }

            string description = DataDrivenFramework.GetPropertyValue(attribute, "Description") as string;

            if(testCaseInfos == null || testCaseInfos.Length == 0)
                return null;

            FactoryInfo factoryInfo = new FactoryInfo(testCaseInfos, description);
            return factoryInfo;
        }
    }
}
